package com.gyx.rocsun.bootstrap;

import java.util.*;

/**
 * @program: rocsun
 * @description:
 * @author: yx.guo
 * @create: 2025-02-18 22:13
 **/
public class RandomizedSet {


    List<Integer> nums;
    Map<Integer, Integer> indices;
    Random random;

    public RandomizedSet() {
        nums = new ArrayList();
        indices = new HashMap<Integer, Integer>();
        random = new Random();
    }

    public boolean insert(int val) {
        if (indices.containsKey(val)) {
            return false;
        }
        int index = nums.size();
        indices.put(val, index);
        nums.add(val);
        return true;
    }

    public boolean remove(int val) {
        if (!indices.containsKey(val)) {
            return false;
        }
        int index = indices.get(val);
        nums.remove(index);
        return true;
    }

    public int getRandom() {
        int index = random.nextInt(nums.size());
        return nums.get(index);
    }

}
